package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.StrTools;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * 服务起始时间-服务截止时间校验 210003980
 * Created by huanghaosheng on 2021/3/15 9:47
 */
public class LocalCheckStartTimeAndEndTime extends AbstractAttrHandler<KiteInst, CustAttrData> {

    private static final ZSmartLogger LOGGER = ZSmartLogger.getLogger(LocalCheckStartTimeAndEndTime.class);

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, CustAttrData data) {
        try {
            if (KiteStringUtils.isEqual("210003980", attr.getFieldName()) && inst.getActionType()
                .equals(ActionType.A)) { //服务起始时间
                KiteAttr endTimeAttr = inst.getAttr("210003981");
                if (KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(endTimeAttr.getValue())) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date startDate = simpleDateFormat.parse(value);
                    Date endDate = simpleDateFormat.parse(endTimeAttr.getValue());
                    if (startDate.after(endDate)) {
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务起始时间必须小于服务截止时间");
                    }
                }
            }

            if (KiteStringUtils.isEqual("210003981", attr.getFieldName()) && inst.getActionType()
                .equals(ActionType.A)) { //服务截止时间
                KiteAttr startTimeAttr = inst.getAttr("210003980");
                if (KiteStringUtils.isNotEmpty(value) && KiteStringUtils.isNotEmpty(startTimeAttr.getValue())) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date endDate = simpleDateFormat.parse(value);
                    Date startDate = simpleDateFormat.parse(startTimeAttr.getValue());
                    if (startDate.after(endDate)) {
                        ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务截止时间必须大于服务起始时间");
                    }
                }
            }
        }
        catch (ParseException e) {
            LOGGER.error("LocalCheckStartTimeAndEndTime-->", e, e.getMessage());
        }

    }
}
